<?php
// 这是系统自动生成的im应用公共文件
use app\im\model\ImUser;
use app\im\model\ImChat;
//获取用户缓存
function getCacheUser($user_id ,$uniacid = '7777')
{
    $key = 'longbing_card_user_' . $user_id;
    if(!hasCache($key ,$uniacid)) return null;
    return getCache($key ,$uniacid);
}

//设置用户缓存数据
function setCacheUser($user_id ,$value ,$uniacid = '7777')
{
    $key = 'longbing_card_user_' . $user_id;
    return setCache ( $key, $value, 3600, $uniacid);
}

//获取用户信息
function getUser($user_id ,$uniacid ='7777')
{
    //判断缓存是否存在    
    $user = getCacheUser($user_id ,$uniacid);
    if(!empty($user)) return $user;
    //生成查询类
    $user_model = new ImUser();
    //获取数据
    $user = $user_model->getUser(['id' => $user_id ,'uniacid' => $uniacid]);
    if(empty($user)) return null;
    setCacheUser($user_id ,$user ,3600,$uniacid);
    return $user;
}

////返回数据处理
//function getWxApiReturnData($result)
//{
//    if(isset($result['data']['page'])) $result['data']['current_page'] = $result['data']['page'];unset($result['data']['page']);
//    if(isset($result['data']['page_count'])) $result['data']['current_page'] = $result['data']['page_count'];unset($result['data']['page_count']);
//    if(isset($result['data']['total'])) $result['data']['current_page'] = $result['data']['total'];unset($result['data']['total']);
//    if(isset($result['data']['total_page'])) $result['data']['current_page'] = $result['data']['total_page'];unset($result['data']['total_page']);
//    return $result;
//}

//获取chat
function longbingGetChatById($chat_id ,$uniacid)
{
    //设置key
    $key = 'longbing_card_chat_new_' . $chat_id;
    //从缓存中获取数据
    $chat = getCache($key ,$uniacid);
    //判断缓存数据是否存在
    if(empty($chat))
    {
        //生成chat操作模型
        $chat_model = new ImChat();
        //获取数据
        $chat = $chat_model->getChatById($chat_id);
        if(!empty($chat)) setCache($key ,$chat ,$uniacid);
    }
    return $chat;
}

//获取chat
function longbingGetChat($user_id ,$customer_id ,$uniacid = 7777 ,$is_create = true)
{
    //判断用户和客户是否是同一人
    if(empty($user_id) || empty($customer_id) || in_array($user_id, [$customer_id])) return false;
    //查找数据
    $chat_model = new ImChat();
    $chat  = $chat_model->getChat($user_id ,$customer_id ,$uniacid);
    if(isset($chat['id'])){
        $key = 'longbing_card_chat_new_' . $chat['id'];
        setCache($key ,$chat ,$uniacid);
    }
    //如果数据不存在
    if(empty($chat) && $is_create){
        //判断用户是否存在
        $user     = longbingGetUser($user_id ,$uniacid);
        if(empty($user)) return false;
        //判断客户是否存在
        $customer = longbingGetUser($customer_id ,$uniacid);
        if(empty($customer)) return false;
        //生成chat数据
        $chat_data = array(
                         'user_id'   => $user_id,
                         'target_id' => $customer_id,
                         'uniacid'   => $uniacid
                     );
        if($chat_model->createChat($chat_data)){
            $chat = longbingGetChat($user_id ,$customer_id ,$uniacid);
        }
    }
    if(isset($chat['chat_id'])) $chat['id'] = $chat['chat_id'];
    return $chat;
}

/**
 * 腾讯聊天签名方法
 *
 * @param $sdkappid
 * @param $key
 * @param $user_id
 * @return mixed
 * @author shuixian
 * @DataTime: 2019/12/10 11:29
 */
function longbing_publics_tim_genSig($sdkappid, $key,$user_id){
    require_once 'TLSSigAPIv2.php';
    $TLSSigAPIv2 = new \Tencent\TLSSigAPIv2($sdkappid, $key);
    return $TLSSigAPIv2->genSig($user_id);
}